package com.woniuxy.rbac.service.impl;

import com.woniuxy.rbac.entity.User;
import com.woniuxy.rbac.mapper.UserMapper;
import com.woniuxy.rbac.mapper.UserPermMapper;
import com.woniuxy.rbac.mapper.UserRoleMapper;
import com.woniuxy.rbac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 老谭（<a href="http://www.woniuxy.com">蜗牛学苑</a>）
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserPermMapper userPermMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public void add(User user, int[] permIds, int[] roleIds) {
        user.setStatus("y");
        userMapper.insert(user);
        for (int permId : permIds) {
            userPermMapper.insert(user.getId(), permId);
        }
        for (int roleId : roleIds) {
            userRoleMapper.insert(user.getId(), roleId);
        }

    }

    @Override
    public List<User> getAll() {
        return userMapper.list();
    }

    @Override
    public void delById(int id) {
        userMapper.del(id);
    }

    @Override
    public User login(String account) {
        return userMapper.getByAccount(account);
    }
}
